package com.lsfb.signln.Leave.AddLeave;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.MyProgressDialog;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.utils.DensityUtils;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;
import com.zgscwjm.lsfbbasetemplate.utils.T;
import com.zgscwjm.lsfbbasetemplate.view.titlebar.LsfbTitleBar;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.aty_leave_select_date)
public class LeaveSelectDateActivity extends LsfbActivity implements CalendarView.OnDateSelectedListener,
        CalendarView.OnMonthChangeListener,
        CalendarView.OnYearChangeListener,
        CalendarView.OnDateLongClickListener {

    /**
     * 日历
     */
    @ViewInject(R.id.calendarView)
    private CalendarView mCalendarView;

    /**
     * 当前年月
     */
    @ViewInject(R.id.tv_curDate)
    private TextView tv_curDate;

    /**
     *
     */
    private MyProgressDialog dialog;

    /**
     * 选中的天数
     */
    private int day_num = 0;

    private int requestCode = 1;

    private List<DateBean> mData;

    private List<Calendar> schemes;

    private int curYear;
    private int curMonth;
    private int curDay;
    private String dates;
    private String start_date;
    private String end_date;
    private String date;
    private Date date_start;
    private Date date_end;


    @Override
    public void initView() {
        LsfbTitleBar titleBar = LittleUtils.setsimpletitlebar(this, "选择免定位日期");
        TextView textView = LittleUtils.rightbtn(this, "确定");

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                compelete();
            }
        });
        titleBar.setRightView(textView, 0, 0, DensityUtils.dp2px(this, 10), 0);

        dialog = new MyProgressDialog(this, "加载中...");

        mData = new ArrayList<>();
        schemes = new ArrayList<>();


        //滚动到指定日期
//        mCalendarView.scrollToCalendar(mCalendarView.getCurYear(), mCalendarView.getCurMonth(), mCalendarView.getCurDay());
        LsfbLog.e("今天：" + mCalendarView.getCurYear() + "-" + mCalendarView.getCurMonth() + "-" + mCalendarView.getCurDay());
        //设置当前年月
        if (mCalendarView.getCurMonth() < 10) {
            tv_curDate.setText(mCalendarView.getCurYear() + "年0" + mCalendarView.getCurMonth() + "月");
        } else {
            tv_curDate.setText(mCalendarView.getCurYear() + "年" + mCalendarView.getCurMonth() + "月");
        }

        mCalendarView.setOnYearChangeListener(this);
        mCalendarView.setOnDateSelectedListener(this);
        mCalendarView.setOnMonthChangeListener(this);
        mCalendarView.setOnDateLongClickListener(this);
    }

    /**
     * 时间选择完成， 确定
     */
    private void compelete() {
        if (mData.size() == 0) {
            T.showShort(this, "请选择免定位时间");
            return;
        }
        int day = 0;
        for (int i = 0; i < mData.size(); i++) {
            if (mData.get(i).getMonth() < 10) {
                if (mData.get(i).getDay() < 10) {
                    date = String.valueOf(mData.get(i).getYear()) + "-0" + String.valueOf(mData.get(i).getMonth()) + "-0" + String.valueOf(mData.get(i).getDay());
                } else {
                    date = String.valueOf(mData.get(i).getYear()) + "-0" + String.valueOf(mData.get(i).getMonth()) + "-" + String.valueOf(mData.get(i).getDay());
                }
            } else {
                if (mData.get(i).getDay() < 10) {
                    date = String.valueOf(mData.get(i).getYear()) + "-" + String.valueOf(mData.get(i).getMonth()) + "-0" + String.valueOf(mData.get(i).getDay());
                } else {
                    date = String.valueOf(mData.get(i).getYear()) + "-" + String.valueOf(mData.get(i).getMonth()) + "-" + String.valueOf(mData.get(i).getDay());
                }
            }
            if (day == 0) {
                dates = date;
                day++;
            } else {
                dates = dates + "," + date;
            }
        }

        date_start = new Date(mData.get(0).getYear() - 1900, mData.get(0).getMonth() - 1, mData.get(0).getDay());
        date_end = new Date(mData.get(0).getYear() - 1900, mData.get(0).getMonth() - 1, mData.get(0).getDay());
        for (int i = 0; i < mData.size(); i++) {
            Date date_nor = new Date(mData.get(i).getYear() - 1900, mData.get(i).getMonth() - 1, mData.get(i).getDay());
            if (date_nor.before(date_start)) {  //开始日期
                date_start = date_nor;
            }

            if (date_nor.after(date_end)) {     //结束日期
                date_end = date_nor;
            }
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        start_date = format.format(date_start);
        end_date = format.format(date_end);

        if (mData.size() > 0) {
            Intent intent = new Intent();
            intent.putExtra("days", mData.size());   //请假天数
            intent.putExtra("dates", dates);    //具体请假日期
            intent.putExtra("start_date", start_date);    //请假开始日期
            intent.putExtra("end_date", end_date);    //请假结束日期
            setResult(requestCode, intent);
            finish();
        } else {
            T.showShort(this, "您还未选择时间");
        }
    }

    @OnClick({R.id.btn_last_month, R.id.btn_next_month})
    public void onClick(View view) {
        String curDate = tv_curDate.getText().toString();
        switch (view.getId()) {
            //上一个月
            case R.id.btn_last_month:
                //选择日期大于当日年份
                if (Integer.parseInt(curDate.substring(0, 4)) > mCalendarView.getCurYear()) {
                    //选择日期等于1月
                    if (Integer.parseInt(curDate.substring(5, 7)) == 1) {
                        curYear = Integer.parseInt(curDate.substring(0, 4)) - 1;
                        curMonth = 12;
                        //选择日期大于1月
                    } else if (Integer.parseInt(curDate.substring(5, 7)) > 1) {
                        curYear = Integer.parseInt(curDate.substring(0, 4));
                        curMonth = Integer.parseInt(curDate.substring(5, 7)) - 1;
                    }
                    //选择日期等于当日年份
                } else if (Integer.parseInt(curDate.substring(0, 4)) == mCalendarView.getCurYear()) {
                    if (Integer.parseInt(curDate.substring(5, 7)) > mCalendarView.getCurMonth()) {
                        curYear = Integer.parseInt(curDate.substring(0, 4));
                        curMonth = Integer.parseInt(curDate.substring(5, 7)) - 1;
                    } else {
                        T.showShort(this, "不可选择本月之前的日期");
                        return;
                    }
                } else {
                    T.showShort(this, "不可选择本月之前的日期");
                    return;
                }
                mCalendarView.scrollToCalendar(curYear, curMonth, 1);
                if (curMonth < 10) {
                    tv_curDate.setText(curYear + "年0" + curMonth + "月");
                } else {
                    tv_curDate.setText(curYear + "年" + curMonth + "月");
                }

                break;
            //下一个月
            case R.id.btn_next_month:
                if (Integer.parseInt(curDate.substring(0, 4)) < mCalendarView.getCurYear() + 50) {
                    if (Integer.parseInt(curDate.substring(5, 7)) == 12) {
                        curMonth = 1;
                        curYear = Integer.parseInt(curDate.substring(0, 4)) + 1;
                    } else if (Integer.parseInt(curDate.substring(5, 7)) < 12) {
                        curMonth = Integer.parseInt(curDate.substring(5, 7)) + 1;
                        curYear = Integer.parseInt(curDate.substring(0, 4));
                    }
                    mCalendarView.scrollToCalendar(curYear, curMonth, 1);
                    if (curMonth < 10) {
                        tv_curDate.setText(curYear + "年0" + curMonth + "月");
                    } else {
                        tv_curDate.setText(curYear + "年" + curMonth + "月");
                    }
                }
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onYearChange(int year) {

    }

    @Override
    public void onMonthChange(int year, int month) {

    }

    /**
     * 点击某个日期
     *
     * @param calendar
     * @param isClick
     */
    @Override
    public void onDateSelected(Calendar calendar, boolean isClick) {

        if (isClick == true) {
            if (mCalendarView.getCurYear() == calendar.getYear()) {
                if (mCalendarView.getCurMonth() == calendar.getMonth()) {
                    if (mCalendarView.getCurDay() <= calendar.getDay()) {
                        int type = 0;
                        if (mData.size() != 0) {
                            for (int i = 0; i < mData.size(); i++) {
                                if (mData.get(i).getYear() == calendar.getYear() && mData.get(i).getMonth() == calendar.getMonth() && mData.get(i).getDay() == calendar.getDay()) {
                                    mData.remove(i);
                                    schemes.add(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), Color.parseColor("#ffffff"), ""));
                                    type = 1;
                                }
                            }
                            if (type == 0) {
                                DateBean bean = new DateBean();
                                bean.setYear(calendar.getYear());
                                bean.setMonth(calendar.getMonth());
                                bean.setDay(calendar.getDay());
                                mData.add(bean);
                                schemes.add(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), Color.parseColor("#0EAE6E"), ""));
                            }
                        } else {
                            DateBean bean = new DateBean();
                            bean.setYear(calendar.getYear());
                            bean.setMonth(calendar.getMonth());
                            bean.setDay(calendar.getDay());
                            mData.add(bean);
                            schemes.add(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), Color.parseColor("#0EAE6E"), ""));
                        }
                        mCalendarView.update();
                        mCalendarView.setSchemeDate(schemes);
                    } else {
                        T.showShort(this, "不可选择本日前的日期");
                    }
                } else if (mCalendarView.getCurMonth() < calendar.getMonth()) {
                    int type = 0;
                    if (mData.size() != 0) {
                        for (int i = 0; i < mData.size(); i++) {
                            if (mData.get(i).getYear() == calendar.getYear() && mData.get(i).getMonth() == calendar.getMonth() && mData.get(i).getDay() == calendar.getDay()) {
                                mData.remove(i);
                                schemes.add(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), Color.parseColor("#ffffff"), ""));
                                type = 1;
                            }
                        }
                        if (type == 0) {
                            DateBean bean = new DateBean();
                            bean.setYear(calendar.getYear());
                            bean.setMonth(calendar.getMonth());
                            bean.setDay(calendar.getDay());
                            mData.add(bean);
                            schemes.add(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), Color.parseColor("#0EAE6E"), ""));
                        }
                    } else {
                        DateBean bean = new DateBean();
                        bean.setYear(calendar.getYear());
                        bean.setMonth(calendar.getMonth());
                        bean.setDay(calendar.getDay());
                        mData.add(bean);
                        schemes.add(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), Color.parseColor("#0EAE6E"), ""));
                    }
                    mCalendarView.update();
                    mCalendarView.setSchemeDate(schemes);
                }
            } else if (mCalendarView.getCurYear() < calendar.getYear()) {
                int type = 0;
                if (mData.size() != 0) {
                    for (int i = 0; i < mData.size(); i++) {
                        if (mData.get(i).getYear() == calendar.getYear() && mData.get(i).getMonth() == calendar.getMonth() && mData.get(i).getDay() == calendar.getDay()) {
                            mData.remove(i);
                            schemes.add(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), Color.parseColor("#ffffff"), ""));
                            type = 1;
                        }
                    }
                    if (type == 0) {
                        DateBean bean = new DateBean();
                        bean.setYear(calendar.getYear());
                        bean.setMonth(calendar.getMonth());
                        bean.setDay(calendar.getDay());
                        mData.add(bean);
                        schemes.add(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), Color.parseColor("#0EAE6E"), ""));
                    }
                } else {
                    DateBean bean = new DateBean();
                    bean.setYear(calendar.getYear());
                    bean.setMonth(calendar.getMonth());
                    bean.setDay(calendar.getDay());
                    mData.add(bean);
                    schemes.add(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), Color.parseColor("#0EAE6E"), ""));
                }
                mCalendarView.update();
                mCalendarView.setSchemeDate(schemes);
            }
        }
    }

    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme(text);
        return calendar;
    }

    @Override
    public void onDateLongClick(Calendar calendar) {

    }
}
